layui.use(['form', 'table', 'layer', 'layedit'], function (form, table, layer, layedit) {

    //提交按钮监听
    form.on("submit(sendMail)", function (data) {
        const formInfo = data.field;
        console.log("发送邮件的信息:", JSON.stringify(formInfo));
        $.ajax({
            type: "post",
            url: "../email/send",
            data: JSON.stringify(formInfo),
            headers: {
                'Content-Type': 'application/json;charset=utf-8'
            },
            success: function (data) {
                console.log("发送邮件响应数据",data);
                if (parseInt(data.obj) > 0) {
                    layer.msg(
                        data.msg,
                        {
                            icon: 1,
                            time: 1000
                        },
                        function () {
                            form.render();
                            window.location.href = "phone-main.html";
                        });
                }else{
                    layer.msg(data.msg ,{icon : 2});
                }
            },
            error: function (o) {
            }
        });
        return false;
    });

    //富文本编辑器图片上传
    layedit.set({
        uploadImage: {
            url: '../upload/picture' //接口url
            , type: '' //默认post
        }
    });

    var mailContent = layedit.build('mailContent'); //建立富文本编辑器

    //得到富文本编辑器内容
    form.verify({
        mailContent: function (value) {
            return layedit.sync(mailContent);
        }
    });

    layer.ready(function () {
        $.ajax({
            url: "../user/" + parseInt(Base64.decode(getToken())),
            success: function (data) {
                //给表单赋值
                form.val("sendMail_form", { //formTest 即 class="layui-form" 所在元素属性 lay-filter="" 对应的值
                    "fromMail": data.obj.mail
                });
            }
        });
    });

    form.render();//重点在这里
});